package proxies.classes.subscription
{
	import core.events.PutIoErrorEvent;
	import core.events.PutIoResultEvent;
	
	import proxies.Proxy;
	import proxies.classes.subscription.events.SubscriptionProxyError;
	import proxies.classes.subscription.events.SubscriptionProxyListResponse;
	
	public class SubscriptionProxy extends Proxy implements ISubscriptionProxy
	{
		// ------------- CONSTANTS ----------------------
		public static const NAME:String = "subscriptions";
		
		
		public static const METHOD_LIST:String = 'list';
		public static const METHOD_CREATE:String = 'create';
		public static const METHOD_EDIT:String = 'edit';
		public static const METHOD_DELETE:String = 'delete';
		public static const METHOD_PAUSE:String = 'pause';
		public static const METHOD_INFO:String = 'info';
		
		public function SubscriptionProxy()
		{
			super(NAME);
		}
		
		public function listSubscriptions():void
		{
			sendRequest(METHOD_LIST, onResult, onError);
		}
		
		public function createSubscription(title:String, url:String, doFilters:String, dontFilters:String, parentFolderId:String, paused:Boolean):void
		{
			sendRequest(METHOD_CREATE,onResult, onError);
		}
		
		public function editSubscription(id:int, title:String, url:String, doFilters:String, dontFilters:String, parentFolderId:String, paused:Boolean):void
		{
			sendRequest(METHOD_EDIT, onResult, onError);
		}
		
		public function deleteSubscription(id:int):void
		{
			sendRequest(METHOD_DELETE, onDeleteSubscription, onError);
		}
		
		public function pauseSubscription(id:int):void
		{
			sendRequest(METHOD_PAUSE, onPauseSubscription, onError);
		}
		
		public function infoSubscription(id:int):void
		{
		}
		
		//-------------------------Action Listeners --------------------------
		private function onError(error:PutIoErrorEvent):void
		{
			//Add error message
			var subscriptionProxyError:SubscriptionProxyError = new SubscriptionProxyError(error.errorMessage, error.bubbles, error.cancelable);
			
			//Fire event
			dispatchEvent(subscriptionProxyError);
		}
		
		private function onResult(result:PutIoResultEvent):void
		{
			// Fetch Data into an Arraylist.
			var itemsList:Array = buildItemList(result.results);
			
			// Put them into another Event and dispatch it.
			var subscriptionProxyListResponse:SubscriptionProxyListResponse = new SubscriptionProxyListResponse(itemsList, result.bubbles, result.cancelable);
			dispatchEvent(subscriptionProxyListResponse);
		}
		
		private function onDeleteSubscription(result:PutIoResultEvent):void
		{
			// Say something to confirm the delete has been okay.
		}
		
		private function onPauseSubscription(result:PutIoResultEvent):void
		{
			// Say something to confirm that the togle made its job.
		}
		
		//-------------------------Tools--------------------------------------
		
		/**
		 * Unpack the data from the event and return a data Array filled with FilesProxyItems.
		 */
		private function buildItemList(result:Object):Array
		{
			var itemsList:Array = new Array();
			var currentItem:SubscriptionProxyItem;
			
			for(var i:int=0; i < result.length; i++)
			{		
				currentItem = new SubscriptionProxyItem(result[i]);
				itemsList.push(currentItem);
			}
			
			return itemsList;
		}		
	}
}